package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.mapper.ShoppingCartMapper;
import com.itheima.pojo.ShoppingCart;
import com.itheima.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Override
    public R add(ShoppingCart shoppingCart, Long userId) {
        shoppingCart.setUserId(userId);
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId()).eq(ShoppingCart::getUserId, shoppingCart.getUserId()).
                eq(shoppingCart.getDishFlavor() != null, ShoppingCart::getDishFlavor, shoppingCart.getDishFlavor());
        wrapper.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId()).eq(ShoppingCart::getUserId, shoppingCart.getUserId());
        ShoppingCart selectOne = shoppingCartMapper.selectOne(wrapper);
        if (selectOne == null) {
            shoppingCartMapper.insert(shoppingCart);
            return R.success(shoppingCart);
        } else {
            selectOne.setNumber(selectOne.getNumber() + 1);
            shoppingCartMapper.updateById(selectOne);
            selectOne.setNumber(null);
            return R.success(selectOne);
        }

    }

    @Override
    public R findByUserId() {
        Long usrId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, usrId);
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(wrapper);
        return R.success(shoppingCarts);
    }

    @Override
    public void clean() {
        Long currentId = BaseContext.getCurrentId();
        LambdaUpdateWrapper<ShoppingCart> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, currentId);
        shoppingCartMapper.delete(wrapper);
    }

    @Override
    public void sub(ShoppingCart shoppingCart) {
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getDishFlavor, shoppingCart.getDishFlavor()).eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        ShoppingCart selectOne = shoppingCartMapper.selectOne(wrapper);
        if (selectOne.getNumber() > 1) {
            selectOne.setNumber(selectOne.getNumber() - 1);
            shoppingCartMapper.updateById(selectOne);
        } else {
            shoppingCartMapper.deleteById(selectOne);
        }
    }
}
